package com.huichenghe.xinlvsh01.Ble;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Handler;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

/**
 * Created by lixiaoning on 15-10-30.
 * 扫描蓝牙工具类
 */
public class BleScanUtils {
    private boolean isScanning;     // 标识正在扫描

    private int LONG_TIME = 10000;
    private int SHOUT_TIME_PRPEAT = 5000;
    private static BleScanUtils mBleScanUtils = null;
    private OnDeviceScanFoundListener mOnDeviceScanFoundListener;
    private Context mContext;
    private Handler mHandler;
    private BleScanUtils(Context mContext)
    {
        this.mContext = mContext;
        mHandler = new Handler(mContext.getMainLooper());

    }



    /**
     * 懒汉式单例模式
     */
    public static BleScanUtils getBleScanUtilsInstance(Context mContext)
    {

        if(mBleScanUtils == null)
        {
            mBleScanUtils = new BleScanUtils(mContext);
        }
        return mBleScanUtils;
    }


    /**
     * 扫描蓝牙设备
     * @param enableScan    为true,开始扫描,false 停止扫描
     * @param isInnerRepeat 为true,表示重复扫描,false只扫一次
     */
    public void scanLeDeviceLoopBegin(boolean enableScan, final boolean isInnerRepeat,
                                      final ContentLoadingProgressBar scanProgress)
    {

        if(enableScan)  // 开始扫描
        {
            if(!isScanning)
            {
                mHandler.postDelayed(new Runnable()     // 10秒后停止扫描,或重新扫描
                {
                    @Override
                    public void run()
                    {
                        if(isScanning)
                        {
                            scanLeDeviceLoopBegin(false, isInnerRepeat, scanProgress);
                            if(isInnerRepeat)
                            {
                                scanLeDeviceLoopBegin(true, isInnerRepeat, scanProgress);
                            }
                        }
                    }
                }, isInnerRepeat ? SHOUT_TIME_PRPEAT : LONG_TIME);
            }
            isScanning = true;
            if(BluetoothAdapter.getDefaultAdapter() != null && BluetoothAdapter.getDefaultAdapter().isEnabled())
            {
                // 开始扫描,工作线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        BluetoothAdapter.getDefaultAdapter().startLeScan(myLeScanCallback);
                    }
                }).start();
                ((Activity)mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        scanProgress.show();
                    }
                });

            }

        }
        else            // 停止扫描
        {
            if(isScanning)
            {
                isScanning = false; // 标识为false
                if(BluetoothAdapter.getDefaultAdapter() != null && BluetoothAdapter.getDefaultAdapter().isEnabled())
                {
                       BluetoothAdapter.getDefaultAdapter().stopLeScan(myLeScanCallback);
                        ((Activity)mContext).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                scanProgress.hide();
                            }
                        });

                }
            }
        }
    }


    /**
     * 扫描后的回调接口,将扫描到的数据作为onLeScan参数回传给实现类
     */
    BluetoothAdapter.LeScanCallback myLeScanCallback = new BluetoothAdapter.LeScanCallback()
    {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
        {

            if(device != null)  // 若扫描到设备，构建实体类
            {
                LocalDeviceEntity deviceEntity = new LocalDeviceEntity(device.getName(), device.getAddress(),rssi, scanRecord);
                mOnDeviceScanFoundListener.OnDeviceFound(deviceEntity);
            }


        }
    };

    /**
     *  扫描设备监听器，对是否扫描到设备进行监听并处理
     */
    public interface OnDeviceScanFoundListener
    {
        public void OnDeviceFound(LocalDeviceEntity mLocalDeviceEntity);
    }


    /**
     * 设置监听
     * @param mOnDeviceScanFoundListener
     */
    public void setmOnDeviceScanFoundListener(OnDeviceScanFoundListener mOnDeviceScanFoundListener)
    {
        this.mOnDeviceScanFoundListener = mOnDeviceScanFoundListener;
    }


}
